package com.cloud.lan.service.sms;

import com.cloud.lan.api.dto.req.MmsTemplateReq;
import com.cloud.lan.api.dto.resp.MmsTemplateResp;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.ListResponse;
import com.cloud.lan.integration.submail.common.CommonService;
import com.cloud.lan.integration.submail.mms.entity.MmsContentElement;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateContent;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateHandleRes;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateModel;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateQueryRes;
import com.cloud.lan.integration.submail.mms.service.MmsTemplateService;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by zhouquan on 2022/4/4.
 */
@Slf4j
@Service
public class MmsTemplateManageService {

    @Autowired
    private MmsTemplateService mmsTemplateService;
    @Autowired
    private CommonService commonService;

    public ListResponse manageList(MmsTemplateModel model){
        ListResponse listResponse = new ListResponse();
        MmsTemplateQueryRes queryRes = mmsTemplateService.query(model);
        List<MmsTemplateResp> respList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(queryRes.getTemplates())){
            queryRes.getTemplates().stream().forEach(te->{
                try{
                    MmsTemplateResp resp = new MmsTemplateResp();
                    BeanUtils.copyProperties(resp,te);
                    respList.add(resp);
                }catch (Exception e){
                    log.error("彩信模板.转换异常",e);
                }

            });
        }
        listResponse.setData(respList);
        listResponse.setNums(respList.size());
        return listResponse;
    }

    public BaseResponse merge(MmsTemplateReq templateReq){
        MmsTemplateModel model = new  MmsTemplateModel();
        List<MmsTemplateContent> contentList = new ArrayList<>();
        try{
            BeanUtils.copyProperties(model,templateReq);
        }catch (Exception e){
            log.error("彩信模板.转换异常",e);
        }
        model.setContentList(contentList);

        //把图片下载下来转换成base64数据
        if(CollectionUtils.isNotEmpty(templateReq.getConList())){
            templateReq.getConList().stream().forEach(cm->{
                try{
                    if(StringUtils.isNotBlank(cm.getUrl())){
                        MmsTemplateContent content = new MmsTemplateContent();
                        content.setText(cm.getText());
                        MmsContentElement image = new MmsContentElement();
                        content.setImage(image);
                        contentList.add(content);

                        String[] rea = cm.getUrl().split("\\.");
                        String endS = rea[rea.length-1];
                        if(StringUtils.equalsIgnoreCase("jpg",endS)){
                            image.setType("image/jpg");
                        }else if(StringUtils.equalsIgnoreCase("png",endS)){
                            image.setType("image/png");
                        }else if(StringUtils.equalsIgnoreCase("jpeg",endS)){
                            image.setType("image/jpeg");
                        }else if(StringUtils.equalsIgnoreCase("gif",endS)){
                            image.setType("image/gif");
                        }
                        String cont = commonService.encodeImageToBase64(cm.getUrl());
                        image.setData(cont);
                    }
                }catch (Exception e){
                    log.info("彩信模板.图片读取异常.error",e);
                }
            });
        }
        if(CollectionUtils.isEmpty(model.getContentList())){
            return BaseResponse.error();
        }
        if(StringUtils.isBlank(model.getTemplateId())){
            //create
            MmsTemplateHandleRes handleRes = mmsTemplateService.create(model);
            BaseResponse baseResponse = BaseResponse.success();
            baseResponse.setData(handleRes);
            return baseResponse;
        }else{
            //update
            if(CollectionUtils.isNotEmpty(model.getContentList())
                    && StringUtils.isNotBlank(model.getMmsSignature())){
                model.getContentList().stream().forEach(co->{
                    if(StringUtils.isNotBlank(co.getText()) && co.getText().contains(model.getMmsSignature())){
                        co.getText().replaceAll(model.getMmsSignature(),"");
                    }
                });

            }
            MmsTemplateHandleRes handleRes = mmsTemplateService.update(model);
            BaseResponse baseResponse = BaseResponse.success();
            handleRes.setTemplateId(model.getTemplateId());
            baseResponse.setData(handleRes);
            return baseResponse;
        }
    }

    public BaseResponse del(MmsTemplateModel model){
        MmsTemplateHandleRes handleRes = mmsTemplateService.del(model);
        BaseResponse baseResponse = BaseResponse.success();
        handleRes.setTemplateId(model.getTemplateId());
        baseResponse.setData(handleRes);
        return baseResponse;
    }
}
